package com.chengzzz.czsee

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chengzzz.czsee.adapter.videoAdapter
import com.chengzzz.czsee.core.BaseAcitivity
import com.chengzzz.czsee.core.infoActivity
import com.chengzzz.czsee.network.ServiceCreator
import com.chengzzz.czsee.network.videoService
import com.chengzzz.czsee.pojo.DataClass


import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlin.concurrent.thread

lateinit var data: DataClass
var index: Int = 1
val appService = ServiceCreator.create<videoService>()

class MainActivity : BaseAcitivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initData()
        //监听列表滑动
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    val layoutManager = recyclerView.layoutManager
                    if (layoutManager is LinearLayoutManager) {
                        val manager: LinearLayoutManager = layoutManager
                        val lastPosition = manager.findLastVisibleItemPosition()
                        if (lastPosition == videoAdapter(data).itemCount - 1) {
                            //最后一条已经显示了
                            slideData()
                        }
                    }
                }
            }
        })

        SearchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener,
            android.widget.SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
             searchData(query)

                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }
        })
    }


    fun initData() {
        thread {
            showloading()
            appService.getVideoInfo(index).enqueue(object : Callback<DataClass> {
                override fun onFailure(call: Call<DataClass>, t: Throwable) {
                    Log.d("MainActivity", " Throwable is eeor" + t.printStackTrace())
                }

                override fun onResponse(call: Call<DataClass>, response: Response<DataClass>) {
                    val list = response.body() as DataClass
                    Log.d(
                        "MainActivity",
                        " pagecount:" + list.page.pagecount + " pagesize:" + list.page.pagesize + " index:" + list.page.pageindex + " data size:" + list.data.size
                    )
                    val layoutManager = LinearLayoutManager(applicationContext)
                    recyclerView.layoutManager = layoutManager
                    data = list
                    val adapter = videoAdapter(list)
                    recyclerView.adapter = adapter
                    hiddenloading()
                    Toast.makeText(this@MainActivity, "加载完成", Toast.LENGTH_SHORT).show()
                }
            })

        }
    }

    fun slideData() {
        thread {
            showloading()
            appService.getVideoInfo(index++)
                .enqueue(object : Callback<DataClass> {
                    override fun onFailure(
                        call: Call<DataClass>,
                        t: Throwable
                    ) {
                        Log.d(
                            "MainActivity",
                            " Throwable is eeor" + t.printStackTrace()
                        )
                    }

                    override fun onResponse(
                        call: Call<DataClass>,
                        response: Response<DataClass>
                    ) {
                        val list = response.body() as DataClass
                        Log.d(
                            "MainActivity",
                            " pagecount:" + list.page.pagecount + " pagesize:" + list.page.pagesize + " index:" + list.page.pageindex + " data size:" + list.data.size
                        )
                        val layoutManager =
                            LinearLayoutManager(applicationContext)
                        recyclerView.layoutManager = layoutManager
                        data = list
                        val adapter = videoAdapter(list)
                        recyclerView.adapter = adapter
                        hiddenloading()
                        Toast.makeText(
                            this@MainActivity,
                            "加载完成",
                            Toast.LENGTH_SHORT
                        ).show()

                    }
                })
        }
    }


    fun searchData(query: String?) {
        thread {
            showloading()
            appService.searchVideoInfo(query as String)
                .enqueue(object : Callback<DataClass> {
                    override fun onFailure(
                        call: Call<DataClass>,
                        t: Throwable
                    ) {
                        Log.d(
                            "MainActivity",
                            " Throwable is eeor" + t.printStackTrace()
                        )
                    }

                    override fun onResponse(
                        call: Call<DataClass>,
                        response: Response<DataClass>
                    ) {
                        val list = response.body() as DataClass
                        Log.d(
                            "MainActivity",
                            " pagecount:" + list.page.pagecount + " pagesize:" + list.page.pagesize + " index:" + list.page.pageindex + " data size:" + list.data.size
                        )
                        val layoutManager =
                            LinearLayoutManager(applicationContext)
                        recyclerView.layoutManager = layoutManager
                        data = list
                        val adapter = videoAdapter(list)
                        recyclerView.adapter = adapter
                        hiddenloading()
                        Toast.makeText(
                            this@MainActivity,
                            "加载完成",
                            Toast.LENGTH_SHORT
                        ).show()

                    }
                })
        }
    }

}








